package com.question.controller.user;

import com.question.annotation.Log;
import com.question.constant.RedisKeyConstant;
import com.question.context.BaseContext;
import com.question.enumeration.MethodType;
import com.question.enumeration.OperatorType;
import com.question.pojo.dto.QuestionPageDTO;
import com.question.pojo.dto.QuestionSubmitDTO;
import com.question.pojo.dto.UserQuestionDTO;
import com.question.pojo.entity.Question;
import com.question.pojo.entity.QuestionCategories;
import com.question.pojo.result.PageResult;
import com.question.pojo.result.Result;
import com.question.pojo.vo.AdviceVO;
import com.question.pojo.vo.QuestionSubmitVO;
import com.question.pojo.vo.UserQuestionVO;
import com.question.pojo.vo.WrongQuestionVO;
import com.question.service.IQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/question")
@Api(tags = "题目相关接口")
@Slf4j
public class UserQuestionController {

    @Autowired
    private IQuestionService questionService;

    /**
     * 题目分页查询
     * @param questionPageDTO
     * @return
     */
    @ApiOperation("分页查询题目")
    @GetMapping("/page")
    @Cacheable(cacheNames = RedisKeyConstant.REDIS_USER_LISTPAGE_KEY, key = "#questionPageDTO.hashCode()")
    @Log(title = "题目分页查询",type = MethodType.SELECT,operatorType = OperatorType.USER)
    public Result<PageResult> listQuestions(QuestionPageDTO questionPageDTO){
        log.info("题目分页查询:{}", questionPageDTO);
        PageResult pageResult = questionService.listPage(questionPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询所有题目
     * 可根据题目内容模糊查询，分类，类型（选择填空），专业 查询
     * @return
     */
    @ApiOperation("查询所有题目")
    @GetMapping("/list")
    @Cacheable(cacheNames = RedisKeyConstant.REDIS_USER_LISTALL_KEY, key = "#questionDTO.hashCode()")
    @Log(title = "查询所有题目",type = MethodType.SELECT,operatorType = OperatorType.USER)
    public Result<List<UserQuestionVO>> listAllQuestions(UserQuestionDTO questionDTO){
        log.info("查询所有题目，条件：{}",questionDTO);
        List<UserQuestionVO> questionVOList = questionService.listAllQuestions(questionDTO);
        return Result.success(questionVOList);
    }

    /**
     * 新增题目
     * @param question
     * @return
     */
    @ApiOperation("新增题目")
    @PostMapping("/save")
    @Caching(evict = {@CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTALL_KEY, allEntries = true),
            @CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTPAGE_KEY, allEntries = true)
    })
    @Log(title = "新增题目",type = MethodType.INSERT,operatorType = OperatorType.USER)
    public Result save(Question question){
        log.info("新增题目：{}",question);

        questionService.save(question);

        return Result.success();
    }

    /**
     * 查询回显
     * @return
     */
    @ApiOperation("查询回显")
    @GetMapping("/getById/{id}")
    public Result<Question> getById(Integer id){
        Question question = questionService.getById(id);
        return Result.success(question);
    }

    /**
     * 用户提交答案
     * @param questionSubmitDTO
     * @return
     */
    @Log(title = "用户提交答案",type = MethodType.SUBMIT,operatorType = OperatorType.USER)
    @ApiOperation("用户提交答案")
    @PostMapping("/submit")
    @Caching(evict = {@CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTALL_KEY, allEntries = true),
            @CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTERROR_KEY, allEntries = true)
    })
    public Result<QuestionSubmitVO> submitAnswer(@RequestBody QuestionSubmitDTO questionSubmitDTO){
        log.info("用户提交答案：{}",questionSubmitDTO);
        QuestionSubmitVO questionSubmitVO = questionService.submitAnswer(questionSubmitDTO);
        return Result.success(questionSubmitVO);
    }

    /**
     * 查询当前用户的所有错题
     * @return
     */
    @ApiOperation("查询当前用户的所有错题")
    @GetMapping("/listWrong")
    @Cacheable(cacheNames = RedisKeyConstant.REDIS_USER_LISTERROR_KEY, key = "T(com.question.context.BaseContext).currentId")
    @Log(title = "查询当前用户的所有错题",type = MethodType.SELECT,operatorType = OperatorType.USER)
    public Result<List<WrongQuestionVO>> listWrongQuestion(){
        log.info("查询当前用户的所有错题");
        List<WrongQuestionVO> wrongQuestionVOS = questionService.listWrongQuestion();
        return Result.success(wrongQuestionVOS);
    }

    // 返回所有题目类型
    @ApiOperation("返回所有题目类型")
    @GetMapping("/getCategory")
    public Result<List<QuestionCategories>> getQuestionCategory(){
        log.info("返回所有题目类型");
        List<QuestionCategories> questionCategories = questionService.getQuestionCategory();
        return Result.success(questionCategories);
    }

    // 根据用户所做的题，给出学习建议
    @ApiOperation("根据用户所做的题，给出学习建议")
    @GetMapping("/getAdvice")
    @Log(title = "给出学习建议",type = MethodType.SELECT,operatorType = OperatorType.USER)
    public Result<AdviceVO> getAdvice(@RequestParam(required = false,defaultValue = "false") Boolean retry){
        log.info("根据用户所做的题，给出学习建议");
        Long userId = BaseContext.getCurrentId();
        AdviceVO advice = questionService.getAdvice(userId,retry);
        return Result.success(advice);
    }

}
